import java.util.Scanner;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
public class ArrayStack {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        String key="";
        boolean loop =true;
        Scanner scanner = new Scanner(System.in);
        while (loop){
            System.out.printf("show: 打印栈\t");
            System.out.printf("push: 入栈\t");
            System.out.printf("pop: 出栈\t");
            System.out.printf("exit: 退出\t\n");
            key=scanner.next();
            switch (key){
                case "show":
                    arrayStack.list();
                    break;
                case "push":
                    System.out.println("请输入一个数:");
                    int value =scanner.nextInt();
                    arrayStack.push(value);
                    break;
                case "pop":
                    try {
                        System.out.println(arrayStack.pop());
                    }catch (RuntimeException e){
                        e.printStackTrace();
                    }
                    break;
                case "exit":
                    loop=false;
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }

    }
    private int maxSize;
    private int[] stack;
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    //判断栈是否满了
    public boolean isFull(){
        return top==maxSize-1;
    }

    //判断栈空
    public boolean isEmpty(){
        return top==-1;
    }

    //入栈
    public void push(int value){
        if (isFull()){
            System.out.println("栈满了!!!");
        } else {
            top++;
            stack[top]=value;
        }
    }

    //出栈
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈为空!!!!");
        } else {
            int value = stack[top];
            top--;
            return value;
        }
    }

    //遍历栈
    public void list(){
        if (isEmpty()){
            System.out.println("没有数据");
        } else {
            System.out.println("==============================");
            for (int i = top; i >=0 ; i--) {
                System.out.printf("stack[%d]=%d\n",i,stack[i]);
            }
            System.out.println("==============================");
        }
    }
}
